package com.yunmang.activity.exception;

import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yunmang.R;
import com.yunmang.activity.BaseActivity;
import com.yunmang.app.AppConstant;
import com.yunmang.utils.SharedPreferencesUtils;
import com.yunmang.view.PopupAdapter;
import com.yunmang.view.PopupButton;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2017/5/18.
 */
public class LeakageInformation extends BaseActivity{
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    private PopupButton btn_look;
    private LayoutInflater inflater;
    private List<String> cValues;
    private ArrayList<String> exception,temptotal;
    private String tag,filename;//标示
    private List<List<String>> cList;
    private int pos;//记录用户点击的position
    private List<String> historyList;
    private String tablename;//操作的表名称
    private ListView pLv,cLv;
    private View view2;
    @Override
    public int getLayoutId() {
        return R.layout.act_lookinfomation;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        btn_look=(PopupButton) findViewById(R.id.btn2);
        inflater = LayoutInflater.from(this);
        view2 = inflater.inflate(R.layout.popup2,null);
        pLv = (ListView) view2.findViewById(R.id.parent_lv);
        cLv = (ListView) view2.findViewById(R.id.child_lv);
        initToolbar();

    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("查看漏扫");
        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    @Override
    public void initData() {
        ArrayList<String> pList =getIntent().getStringArrayListExtra("leakinformation");
        temptotal=getIntent().getStringArrayListExtra("totalNumbers");
        tag=getIntent().getStringExtra("Tag");
        filename=getIntent().getStringExtra("filename");
        String history= SharedPreferencesUtils.getParameter(this,filename);
        String[] histroyArray=history.split(",");
        historyList = new ArrayList<String>(Arrays.asList(histroyArray));
        cList = new ArrayList<List<String>>();

        for(int i = 0; i <pList.size(); i ++) {
            List<String> t = new ArrayList<String>();
            String sub = "";
            for(int m=1;m<=Integer.parseInt(temptotal.get(i));m++){
                sub = String.format("%3s", m).replace(' ', '0');
                t.add(pList.get(i)+sub);
            }
            t.removeAll(historyList);//实现了差集
            cList.add(t);
        }

        cValues = new ArrayList<String>();
        cValues.addAll(cList.get(0));
        final PopupAdapter pAdapter = new PopupAdapter(this,R.layout.popup_item,pList,R.drawable.normal,R.drawable.press2);
        final PopupAdapter cAdapter = new PopupAdapter(this,R.layout.popup_item,cValues,R.drawable.normal,R.drawable.press);
        pAdapter.setPressPostion(0);

        pLv.setAdapter(pAdapter);
        cLv.setAdapter(cAdapter);
        btn_look = (PopupButton) findViewById(R.id.btn2);
        btn_look.setPopupView(view2);
        pLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pos=position;
                System.out.println("位置："+pos+"\n"+"数目："+temptotal.get(pos));
                pAdapter.setPressPostion(position);
                pAdapter.notifyDataSetChanged();
                cValues.clear();
                cValues.addAll(cList.get(position));
                cAdapter.notifyDataSetChanged();
                cAdapter.setPressPostion(-1);
                cLv.setSelection(0);
            }
        });
    }
}
